package com.qcxy.gzy.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qcxy.gyz.consts.CMDConts;
import com.qcxy.gzy.activity.base.BaseActivity;
import com.qcxy.gzy.bean.ComReceiveDataBean;
import com.qcxy.gzy.view.CircleChartView;
import com.qxcy.gzy.util.DataCovertUtil;
import com.qxcy.gzy.util.SPUtils;

public class DetectionActivity extends BaseActivity implements OnClickListener {

    private TextView mJiLiTest;
    private TextView mJuLiTest;

    /**
     * 用于界面显示的变量
     **/
    private int chartViewStrengthValue = 0;
    private int chartViewMaxStrengthValue = 0;
    private double chartViewDistanceValue = 0;
    private double chartViewMaxDistanceValue = 0;

    /**
     * 仪表盘View
     */
    private CircleChartView mDialChartView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detection);

        findViewById(R.id.iv_back).setOnClickListener(this);
        mJiLiTest = (TextView) findViewById(R.id.jili_test);
        mJuLiTest = (TextView) findViewById(R.id.juli_test);

        mDialChartView = (CircleChartView) findViewById(R.id.myview_circle_chart);

        mJiLiTest.setOnClickListener(this);
        mJuLiTest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                SPUtils.setValueSharedFile(DetectionActivity.this, "MAX_STRENGTH", String.valueOf(MAX_STRENGTH));
                SPUtils.setValueSharedFile(DetectionActivity.this, "MAX_DISTANCE", String.valueOf(MAX_DISTANCE));
                MAX_STRENGTH = 0;
                MAX_DISTANCE = 0;
                DetectionActivity.this.finish();
                break;

            case R.id.jili_test:
                if (mJiLiTest.getText().toString().equals("检测中...")) {
                    break;
                } else {
                    mDialChartView.setId(1);
                    mJiLiTest.setText(R.string.runing);
                    mJuLiTest.setText(R.string.jl_detection);
                    addMuscleStrengthTestData();
                    break;
                }

            case R.id.juli_test:
                if (mJuLiTest.getText().toString().equals("检测中...")) {
                    break;
                } else {
                    mDialChartView.setId(2);
                    mJuLiTest.setText(R.string.runing);
                    mJiLiTest.setText(R.string.jl_test);
                    addDistanceDetectData();
                    break;
                }
            default:
                break;
        }
    }

    /**
     * 添加肌力测试数据
     */
    private void addMuscleStrengthTestData() {
        closeSendThread();
        List<byte[]> cmdList = new ArrayList<byte[]>();
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F0);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F1);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F2);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F3);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F4);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_F5);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D0);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D1);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D2);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D3);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D4);
        cmdList.add(CMDConts.CMD_WRITE_F_REIGSTER_D5);
        cmdList.add(CMDConts.CMD_READ_REGISTER_3);
        // 开启发送数据
        mSendThread = new SendThread(cmdList, false);
        mSendThread.start();
    }

    /**
     * 添加距离检测数据
     */
    private void addDistanceDetectData() {
        closeSendThread();
        List<byte[]> cmdList = new ArrayList<byte[]>();
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F0);
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F1);
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F2);
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F3);
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F4);
        cmdList.add(CMDConts.CMD_WRITE_D_REIGSTER_F5);
        cmdList.add(CMDConts.CMD_READ_REGISTER_4);
        mSendThread = new SendThread(cmdList, false);
        mSendThread.start();
    }

    @Override
    public void onDataReceived(ComReceiveDataBean ComRecData) {
        if (ComRecData.getCRC(ComRecData.bRec).equals("0")) {
            // 接收到命令发送过来的数据
            final int msg = DataCovertUtil.HexToInt(DataCovertUtil.ByteArrToHex(ComRecData.bRec).substring(6, 10));
            final int control = DataCovertUtil.HexToInt(DataCovertUtil.ByteArrToHex(ComRecData.bRec).substring(2, 4));
            Log.d(TAG, "onDataReceived Read:" + DataCovertUtil.ByteArrToHex(ComRecData.bRec));
            if (control == 3) {// 3表示：读
                // 瞬时值初始化单位牛顿
                int instantaneousValueN = msg;
                int chartViewID = mDialChartView.getId();
                switch (chartViewID) {
                    case 1: {
                        // 牛顿转换为公斤
                        int instaneousValueKg = (int) 1.0 * instantaneousValueN * 10 / 333;
                        Log.d(TAG, "onDataReceived instantaneousValueN:" + instantaneousValueN + ",instaneousValueKg:" + instaneousValueKg);

                        Log.d(TAG, "------>max:" + MAX_STRENGTH);
                        if (instaneousValueKg > MAX_STRENGTH) {// 实时值大于保存的最大值
                            // 保存最大值
                            MAX_STRENGTH = instaneousValueKg;
                        }
                        // 仪表盘数据
                        chartViewStrengthValue = (int) (instaneousValueKg * 100 / 100);
                        chartViewMaxStrengthValue = (int) (MAX_STRENGTH * 100 / 100);
                        Log.d(TAG, "onDataReceived chartViewStrengthValue:" + chartViewStrengthValue);
                        // 更新UI
                        runOnUiThread(new Runnable() {
                            public void run() {
                                mDialChartView.setPercent(chartViewStrengthValue, chartViewMaxStrengthValue);
                                mDialChartView.setText(String.valueOf(chartViewStrengthValue));
                            }
                        });
                        break;
                    }
                    case 2: {
                        if (instantaneousValueN > MAX_DISTANCE) {// 实时值大于保存的最大值
                            MAX_DISTANCE = instantaneousValueN;
                            // 保存最大值
                            Log.d(TAG, "----->instantaneousValueN:" + instantaneousValueN);
                        }

                        // 仪表盘数据
                        chartViewDistanceValue = instantaneousValueN * 100 / 3500;
                        chartViewMaxDistanceValue = MAX_DISTANCE * 100 / 3500;
                        Log.d(TAG, "onDataReceived chartViewDistanceValue:" + chartViewDistanceValue + ",maxValue:" + chartViewMaxDistanceValue);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                mDialChartView.setPercent(chartViewDistanceValue, chartViewMaxDistanceValue);
                                mDialChartView.setText(String.valueOf((int) chartViewDistanceValue));
                            }
                        });
                        break;
                    }
                    default:
                        break;
                }
            }
        }
    }
}
